﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AxSHDocVw;
using mshtml;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;

namespace Insurance_Quotation_App
{
    public static class China
    {
        //private static AxWebBrowser browser = null;
        //private static AxWebBrowser popup = null;
        private static AxWebBrowser browser = null;
        private static AxWebBrowser popup = null;

        public static void Run(AxWebBrowser webBrowser1, AxWebBrowser webBrowser2)
        {
            browser = webBrowser1;
            popup = webBrowser2;
            browser.Navigate("http://www.sg.cntaiping.com/chinainsB2B/Login.aspx");
            browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(browser_DocumentComplete);
        }

        static void browser_DocumentComplete(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            browser.DocumentComplete -= browser_DocumentComplete;

            Autofill.SetText(browser, "name", "txtUserID", "BR0001A-01");
            Autofill.SetText(browser, "name", "txtPassword", "ACCLAIM02");

            browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(browser_DocumentComplete2);
            Autofill.ClickButton(browser, "name", "cmdLogin", "input");
        }


        private static void browser_DocumentComplete2(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            browser.DocumentComplete -= browser_DocumentComplete2;

            browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(browser_DocumentComplete3);
            
        }

        private static void browser_DocumentComplete3(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            browser.DocumentComplete -= browser_DocumentComplete3;

            browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(browser_DocumentComplete4);

        }

        private static void browser_DocumentComplete4(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            browser.DocumentComplete -= browser_DocumentComplete4;

            browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(browser_DocumentComplete5);
        }
        private static void browser_DocumentComplete5(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            browser.DocumentComplete -= browser_DocumentComplete5;

            browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(browser_DocumentComplete6);
            browser.Navigate("http://www.sg.cntaiping.com/chinainsB2B/Motor/Private/PteCarQuote.aspx");
        }

        private static void browser_DocumentComplete6(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            browser.DocumentComplete -= browser_DocumentComplete6;
            Autofill.SetSelectList(browser, "name", "cboCoverage", "C-11", true);   // C-11
            browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(browser_DocumentComplete7);

        }
        private static void browser_DocumentComplete7(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            
            browser.DocumentComplete -= browser_DocumentComplete7;

            Autofill.SetText(browser, "name", "txtName2", "Shawn Lin");
            Autofill.SetText(browser, "name", "txtDOB", "07-08-1984");
            Autofill.SetText(browser, "name", "C_MOTOR_PRCLAIMSEXP", "49");     // Oct 2010 ~ Sep 2011 (No Claims with NCD)
            Autofill.SetText(browser, "name", "txtPassDate", "07-08-2003");
            Autofill.SetRadio(browser, "name", "sex", "M");
            Autofill.SetRadio(browser, "name", "occupation", "occNo");
            Autofill.SetText(browser, "name", "MOTOR_PROCCUPATION", "Professional gamer");
            Autofill.SetText(browser, "name", "txtRegNo", "SKD1337");
            Autofill.SetText(browser, "name", "txtRegYr", "2010");

            Autofill.SetSelectList(browser, "name", "C_MOTOR_PRVEHMAKE", "266", false);    // Toyota

            //browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(browser_DocumentComplete8);
            browser.NewWindow2 += new DWebBrowserEvents2_NewWindow2EventHandler(browser_NewWindow2);
            clickButton(1);

            //Autofill.SetSelectList(browser, "name", "cboVehModel", "TY07", false);

            //axBrowser = (SHDocVw.WebBrowser)browser.ActiveXInstance;
            //axBrowser.NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(axBrowser_NewWindow3);

            //IHTMLWindow2 window = ((IHTMLDocument2)(browser.Document)).parentWindow;
            //Console.WriteLine(window.length);
            //IHTMLWindow2 dialog = window.external;
            //window.external.close();
        }
        
        static void browser_NewWindow2(object sender, DWebBrowserEvents2_NewWindow2Event e)
        {
            //popup.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(popup_DocumentComplete);
            browser.NewWindow2 -= browser_NewWindow2;
            popup.RegisterAsBrowser = true;
            e.ppDisp = popup.Application;
            
            popup.Navigate("http://www.sg.cntaiping.com/chinainsB2B/Motor/Common/QueryModelDetails.aspx?TYPE=P&VEHMAKE=Toyota");
            Autofill.SetSelectList(popup, "name", "cboVehModel", "TY07", true);
        }
                
        
        //static void axBrowser_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
        //{
        //    Cancel = true;
        //    //browser.Navigate("http://www.sg.cntaiping.com/chinainsB2B/Motor/Common/QueryModelDetails.aspx?TYPE=P&VEHMAKE=Toyota");
        //    Autofill.SetSelectList(browser, "name", "cboVehModel", "TY07", true);
            
            
        //}
        
        private static void clickButton(int buttonIndex)    // many similar "Click Here" icons with no particular way of identification
                                                            // in China Insurance website
        {
            List<IHTMLElement> images = new List<IHTMLElement>();
            IHTMLElementCollection col = ((IHTMLDocument3)(browser.Document)).getElementsByTagName("img");

            foreach (IHTMLElement element in col)
            {
                if (element.getAttribute("href").Equals("http://www.sg.cntaiping.com/chinainsB2B/Images/ClickHere.gif"))
                {
                    images.Add(element);
                }
            }

            images[buttonIndex].click();
        }
    }
}
